package com.xiangge.stu.system.dao.impl;

import com.xiangge.stu.system.dao.StudentDao;
import com.xiangge.stu.system.domain.Student;

import java.util.ArrayList;

/*
    操作层
        操作数组
            1.准备一个数组
            2.数组中初始化几条数据
 */
public class StudentDaoImpl implements StudentDao {
    // 准备一个数组
    public static final ArrayList<Student> STU_LIST = new ArrayList<Student>();

    // 数组中初始化几条数据,static静态代码块，程序运行就会输出
    static {
        STU_LIST.add(new Student("001","张三",20,"2000-12-12"));
        STU_LIST.add(new Student("002","李四",21,"2000-12-13"));
        STU_LIST.add(new Student("003","王五",20,"2000-12-14"));
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @Override
    public boolean add(Student student) {
        return false;
    }

    @Override
    public boolean delete(String id) {
        // 找到指定id的学生在集合中的位置
        int stuIndex = findById(id);
        // 判断
        if (stuIndex == -1){
            return false;
        }
        // 将集合中stuIndex位置的学生对象删除
        Student remove = STU_LIST.remove(stuIndex);
        return remove == null ? false : true;
    }

    @Override
    public boolean update(Student student) {
        return false;
    }

    @Override
    public void findAll() {

    }

    @Override
    public int findById(String id) {
        int index;
        // 遍历集合
        for (int i = 0; i < STU_LIST.size(); i++) {
            // 获取集合中每一个学生对象
            Student student = STU_LIST.get(i);
            // 判断student的id是否和传过来的id相同
            if (student.getSid().equals(id)){
                index = i;
            }
        }
        return 0;
    }
}
